package org.jeecg.modules.workflow.controller.task;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.workflow.vo.definition.process.BpmProcessDefTodoVo;
import org.jeecg.common.workflow.vo.definition.process.BpmProcessDefinitionListReqVO;
import org.jeecg.common.workflow.vo.definition.process.BpmProcessTodoVo;
import org.jeecg.common.workflow.vo.task.instance.*;
import org.jeecg.modules.workflow.controller.BaseController;
import org.jeecg.modules.workflow.service.task.BpmProcessInstanceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "管理后台 - 流程实例") // 流程实例，通过流程定义创建的一次“申请”
@RestController
@RequestMapping("/bpm/process-instance")
@Validated
public class BpmProcessInstanceController extends BaseController {

    @Resource
    private BpmProcessInstanceService processInstanceService;

    @PostMapping("/create")
    @ApiOperation("新建流程实例")
    //@RequiresPermissions("bpm:process-instance:query")
    public Result<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
        return Result.OK("",processInstanceService.createProcessInstance(getUserId(), createReqVO));
    }

    @DeleteMapping("/cancel")
    @ApiOperation(value = "取消流程实例", notes = "撤回发起的流程")
    //@RequiresPermissions("bpm:process-instance:cancel")
    public Result<Boolean> cancelProcessInstance(@Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) {
        processInstanceService.cancelProcessInstance(getUserId(), cancelReqVO);
        return Result.OK(true);
    }

    @GetMapping("/my-page")
    @ApiOperation(value = "获得我的实例分页列表", notes = "在【我的流程】菜单中，进行调用")
    //@RequiresPermissions("bpm:process-instance:query")
    public Result<IPage<BpmProcessInstancePageItemRespVO>> getMyProcessInstancePage(
            @Valid BpmProcessInstanceMyPageReqVO pageReqVO) {
        IPage<BpmProcessInstancePageItemRespVO> pageResult = processInstanceService.getMyProcessInstancePage(getUserId(), pageReqVO);
        return Result.OK(pageResult);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得指定流程实例", notes = "在【流程详细】界面中，进行调用")
    @ApiImplicitParam(name = "id", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    //@RequiresPermissions("bpm:process-instance:query")
    public Result<BpmProcessInstanceRespVO> getProcessInstance(@RequestParam("id") String id) {
        return Result.OK(processInstanceService.getProcessInstanceVO(id));
    }

    @GetMapping("/todo-instance/map")
    @ApiOperation("获取 待办任务 对应实例 map集合")
    //@RequiresPermissions("bpm:task:query")
    public Result<Map<String, List<BpmProcessTodoVo>>> getTodoProDefMap(BpmProcessDefinitionListReqVO listReqVO) {

        Map<String, List<BpmProcessTodoVo>> mapResult = processInstanceService.getTodoProInsMap(getUserId(),listReqVO);

        return Result.OK(mapResult);
    }

    private String getUserId()
    {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }

}
